const { memberInfo, login, memberPhone } = require('../../utils/api')
const app = getApp()
// pages/login.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        showRegister: false,
        sessionKey: '',
        userInfo: null,
        getUserInfo: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        wx.hideHomeButton()
        this.wxLogin()
    },
    wxLogin: function() {
        let _this = this
        wx.login({
            success(res) {
                login({
                    code: res.code
                }).then(res => {
                    _this.setData({
                        sessionKey: res.sessionKey
                    })
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},

    /**
     * 用户点击右上角分享
     */
    bindgetuserinfo(options) {
        let { detail } = options
        console.log(detail)
        if (detail.errMsg === 'getUserInfo:ok') {
            let obj = {
                area: app.globalData.currentAddress.ad_info.adcode,
                sessionKey: this.data.sessionKey,
                signature: detail.signature,
                rawData: detail.rawData,
                encryptedData: detail.encryptedData,
                iv: detail.iv
            }

            memberInfo(obj).then(res => {
                this.setData({
                    userInfo: res
                })

                if (!res.mobile) {
                    this.setData({
                        showRegister: true,
                        getUserInfo: detail
                    })
                } else {
                    wx.setStorageSync('userInfo', res)
                    app.globalData.userInfo = res
                    wx.reLaunch({
                        url: '/pages/index/index'
                    })
                }
            })
        } else {
            wx.showToast({
                title: '请同意授权',
                icon: 'none',
                duration: 1000
            })
        }
    },
    getPhoneNumber(options) {
        let { detail } = options
        if (detail.errMsg === 'getPhoneNumber:ok') {
            let obj = {
                area: app.globalData.currentAddress.ad_info.adcode,
                sessionKey: this.data.sessionKey,
                signature: this.data.getUserInfo.signature,
                rawData: this.data.getUserInfo.rawData,
                encryptedData: detail.encryptedData,
                iv: detail.iv,
                openid: this.data.userInfo.openid
            }
            memberPhone(obj).then(res => {
                let userInfo = this.data.userInfo
                userInfo = Object.assign({}, userInfo, {
                    mobile: res.phoneNumber
                })
                wx.setStorageSync('userInfo', userInfo)
                app.globalData.userInfo = userInfo
                wx.reLaunch({
                    url: '/pages/index/index'
                })
            })
        } else {
            wx.showToast({
                title: '请同意授权',
                icon: 'none',
                duration: 1000
            })
        }
    },
    jumpToAgreement() {
        wx.navigateTo({
            url: '/pages/login/agreement'
        })
    }
})
